<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
/*use App\Models\Details;*/

use App\Models\Detail;
use App\Models\Cart;
use App\Models\Goods;
use App\Traits\ApiResponse;
use Illuminate\Http\Request;
use App\Models\Match;
use DB;

class CartController extends Controller
{

    use ApiResponse;

    public function store(Request $request)
    {

        // 验证请求的数据
        $this->validate($request, [
            'goods_match' => 'required|array',
        ], [
            'goods_match.*' => '请输入正确的参数'
        ]);
        $match = Match::query()->whereIn('id', array_keys($request['goods_match']))->get();
        if ($match->count() == 0) {
            return $this->error('');
        }
        $user = $request->user();
        foreach ($match as $item) {
            $num = $request['goods_match'][$item->id] ?? 0;
            if ($num == 0) {
                continue;
            }
            Cart::query()->updateOrCreate([
                'user_id' => $user->id,
                'goods_match_id' => $item->id,
                'goods_id' => $item->goods_id,
            ], [
                'num' => $num,
                /*'totalprice'=> $item->price*$num,*/
            ]);
        }
        return $this->success();
    }

    public function index(Request $request)
    {

        $user = $request->user();
        $goodsMatch = Cart::query()
            ->leftJoin('tb_match', 'tb_match.id', '=', 'tb_cart.goods_match_id')
            ->leftJoin('tb_goods', 'tb_goods.goods_id', '=', 'tb_cart.goods_id')
            ->where('user_id', $request->user()->id)
            ->select('tb_cart.num', 'tb_cart.id', 'tb_match.color', 'tb_match.size', 'tb_goods.pic', 'tb_goods.name', 'tb_goods.price', 'tb_cart.is_checked')
            ->get();

        return view('cart.index', [
            'goodsMatch' => $goodsMatch
        ]);
    }

    public function getList(Request $request)
    {
        $goodsMatch = Cart::query()
            ->leftJoin('tb_match', 'tb_match.id', '=', 'tb_cart.goods_match_id')
            ->leftJoin('tb_goods', 'tb_goods.goods_id', '=', 'tb_cart.goods_id')
            ->where('user_id', $request->user()->id)
            ->select('tb_cart.num', 'tb_cart.id', 'tb_match.color', 'tb_match.size', 'tb_goods.pic',
                'tb_goods.name', 'tb_goods.price', 'tb_cart.is_checked', 'tb_cart.goods_match_id')
            ->get();
        return $this->success(['goodsMatch' => $goodsMatch]);
    }

    public function check(Request $request)
    {
        Cart::query()
            ->where('user_id', $request->user()->id)
            ->where('id', $request['id'])
            ->where('user_id', $request->user()->id)->update(['is_checked' => $request['is_checked'] == 'true' ? 1 : 0]);
    }

    public function multipleCheck(Request $request)
    {
        $this->validate($request, [
            'ids' => 'required|array',
            'ids.*' => 'nullable|integer',
        ], [
            'goods_match.*' => '请输入正确的参数'
        ]);
        Cart::query()->where('user_id', $request->user()->id)->update(['is_checked' => 0]);
        if (count($request['ids']) > 0) {
            Cart::query()->where('user_id', $request->user()->id)->whereIn('id', $request['ids'])->update(['is_checked' => 1]);
        }
    }
}
